import configparser
import getopt
import os
import sys
import time
import csv
import allure
import pytest
from selenium import webdriver
from selenium.webdriver.common.service import Service
from selenium.webdriver.support.wait import WebDriverWait
# from webdriver_manager.chrome import ChromeDriverManager

from my_conf.chrome_options import options_conf


@pytest.fixture(scope='session', autouse=True)
def get_argv():
    # env = getopt.getopt(sys.argv, '-h')
    # print('当前运行环境为：'+env[1][1])
    # return env[1][1]
    return "SIT"


@pytest.fixture(scope='session', autouse=True)
def web_driver():
    global driver
    driver = webdriver.Chrome(options=options_conf())
    driver.set_window_size(1920, 1080)
    yield driver
    #driver.quit()
    print("test end!")


@pytest.fixture(scope='session', autouse=True)
def get_server_info(get_argv):
    conf = configparser.ConfigParser()
    conf.read(os.path.dirname(__file__) + '/my_conf/server.ini')
    url = conf.get(get_argv, 'url')
    user = conf.get(get_argv, 'user')
    # user_tran = conf.get(get_argv, 'user_tran')
    # user_tran_cd = conf.get(get_argv, 'user_tran_cd')
    pwd = conf.get(get_argv, 'pwd')
    return {
        "url": url,
        "user": user,
        # "user_tran": user_tran,
        # "user_tran_cd": user_tran_cd,
        "pwd": pwd
    }


# Oracle
@pytest.fixture(scope='session', autouse=False)
def get_db_info(get_argv):
    conf = configparser.RawConfigParser()
    conf.read(os.path.dirname(__file__) + '/my_conf/db_info.ini', encoding='utf-8')
    all = conf.items(get_argv)
    return {
        "db_name_bd": all[0][1],
        "db_name_scf": all[1][1],
        "connectInfo_bd": all[2][1],
        "connectInfo_scf": all[3][1],
        "custCode": all[4][1],
        "custID": all[5][1],
        "customer": all[6][1],
        "debt_pool_id": all[7][1],
    }


# Mysql
@pytest.fixture(scope='session', autouse=False)
def get_db_info_Mysql(get_argv):
    conf = configparser.RawConfigParser()
    conf.read(os.path.dirname(__file__) + '/my_conf/db_info.ini', encoding='utf-8')
    all = conf.items(get_argv)
    return all  # all[0:oms,1:tms,2:bms][0:标签名,1:链接信息]


# 输出测试通过率
def outputExc(env):
    with open(os.path.dirname(__file__) + '/report/allure_report_{}/data/behaviors.csv'.format(env), 'r', newline='',
              encoding='utf-8') as f:
        reader = csv.reader(f)
        # 读取指定行数据
        rows = [row for row in reader]
        failed = rows[1][3]
        broken = rows[1][4]
        passed = rows[1][5]
        skipped = rows[1][6]
        unknown = rows[1][7]
        rows[0].append('PASSRATE')
        rows[1].append(
            '{:.2%}'.format(int(passed) / (int(failed) + int(broken) + int(passed) + int(skipped) + int(unknown))))
    with open(os.path.dirname(__file__) + '/report/allure_report_{}/data/behaviors.csv'.format(env), 'w', newline='',
              encoding='utf-8') as f:
        write = csv.writer(f)
        write.writerows(rows)


if __name__ == '__main__':
    # conf = configparser.ConfigParser()
    # conf.read(os.path.dirname(__file__) + '/my_conf/db_info.ini',encoding='utf-8')
    # A = conf.items('SIT')
    # print(A)
    # print(A[0][1])
    a = get_db_info()
    print(a)
